/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.view;

import br.jm.financeiro.datamodel.LazyDdi;
import br.jm.financeiro.entity.CodigoDdi;
import br.jm.financeiro.sistema.eao.CodigoDdiEao;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author joao
 */
public class CodigoDdiBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private CodigoDdi selectedCodigoDdi;
    private LazyDataModel<CodigoDdi> lazyModel;
    private final CodigoDdiEao codigoDdiJpa;

    public CodigoDdiBean() {
        codigoDdiJpa = CodigoDdiEao.getInstance();
        selectedCodigoDdi = new CodigoDdi();

        lazyModel = new LazyDdi(codigoDdiJpa);
    }

    public void inserir(ActionEvent actionEvent) {
        FacesMessage msg = null;

        try {
            selectedCodigoDdi.setIdDdi(null);
            codigoDdiJpa.create(selectedCodigoDdi);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Código D.D.I. Inserido com Sucesso: ".concat(selectedCodigoDdi.getDescricao())),selectedCodigoDdi.getDescricao());

        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Inserir" + selectedCodigoDdi.getDescricao(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void alterar(ActionEvent actionEvent) {
        FacesMessage msg = null;

        try {
            codigoDdiJpa.edit(selectedCodigoDdi);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Código D.D.I. Alterado com Sucesso: ".concat(selectedCodigoDdi.getDescricao())),selectedCodigoDdi.getDescricao());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Alterar: " + selectedCodigoDdi.getDescricao(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void excluir(ActionEvent actionEvent) {
        FacesMessage msg = null;

        try {
            codigoDdiJpa.destroy(selectedCodigoDdi);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Código D.D.I. Excluido com Sucesso: ".concat(selectedCodigoDdi.getDescricao())),selectedCodigoDdi.getDescricao());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Excluir: " + selectedCodigoDdi.getDescricao(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public LazyDataModel<CodigoDdi> getLazyModel() {
        return lazyModel;
    }

    public void setLazyModel(LazyDataModel<CodigoDdi> lazyModel) {
        this.lazyModel = lazyModel;
    }

    public CodigoDdi getSelectedCodigoDdi() {
        return selectedCodigoDdi;
    }

    public void setSelectedCodigoDdi(CodigoDdi selectedCodigoDdi) {
        this.selectedCodigoDdi = selectedCodigoDdi;
    }

    public void preparaInserir(ActionEvent actionEvent) {
        selectedCodigoDdi = new CodigoDdi();
    }
}
